import DexieCtx from "@/utils/dexiectx";
// projects: '++id, route, name, icon, description, strategy, bot, channel, gameUrl, inviteUrls',
const defaultProjectsData = [
  {
    bot: "sidekick_fans_bot",
    name: "sidekick",
    channel: "sidekick_announcements",
    route: "/sidekick/index",
    strategy: "自动游戏",
    icon: "el-icon-game-sidekick",
    gameUrl: "https://game.sidekick.fans",
  },
  {
    bot: "Tomarket_ai_bot",
    name: "tomarket",
    channel: "tomarket_ai",
    route: "/tomarket/index",
    strategy: "自动游戏",
    icon: "el-icon-game-tomarket",
    gameUrl: "https://mini-app.tomarket.ai",
  },
  {
    bot: "major",
    name: "major",
    channel: "starsmajor",
    route: "/major/index",
    strategy: "自动任务",
    icon: "el-icon-game-major",
    gameUrl: "https://major.bot",
  },
  {
    bot: "catsgang_bot",
    name: "cats",
    channel: "Cats_housewtf",
    route: "/cats/index",
    strategy: "自动每日签到、任务",
    icon: "el-icon-game-cats",
    gameUrl: "https://cats-frontend.tgapps.store",
  },
  {
    bot: "hamster_kombat_bot",
    channel: "hamster_kombat",
    name: "hamster",
    route: "/hamster/index",
    strategy: "每日签到、自动点击、升级各种卡片",
    icon: "el-icon-game-hamster",
    // 游戏启动url必填
    gameUrl: "",
  },
  {
    bot: "PiggyPiggyofficialbot",
    // channel: "hamster_kombat", piggy没有channel
    name: "piggy",
    route: "/piggy/index",
    strategy: "自动任务、天使宝藏，每日签到",
    icon: "el-icon-game-piggy",
    gameUrl: "",
  },
  {
    bot: "BlumCryptoBot",
    name: "blum",
    channel: "blumcrypto",
    route: "/blum/index",
    strategy: "自动游戏、到点收矿挖矿、每日任务、答题",
    icon: "el-icon-game-blum",
    gameUrl: "",
  },
  {
    bot: "tonstationgames_bot",
    name: "tonstation",
    channel: "tonstationgames",
    route: "/tonstation/index",
    strategy: "自动游戏、每日签到、挖矿",
    icon: "el-icon-game-tonstation",
    gameUrl: "",
  },
  {
    bot: "DuckChain_bot",
    name: "duckchain",
    channel: "DuckChainAnn",
    route: "/duckchain/index",
    strategy: "自动游戏、每日签到、挖矿",
    icon: "el-icon-game-duck",
    gameUrl: "",
  },
  {
    bot: "TimeFarmCryptoBot",
    name: "timefarm",
    channel: "@TimeFarmChannel",
    route: "/timefarm/index",
    strategy: "自动游戏、每日签到、挖矿",
    icon: "el-icon-game-timefarm",
    gameUrl: "",
  },
  {
    bot: "tabizoobot",
    name: "tabizoo",
    channel: "tabi_ann",
    route: "/tabizoo/index",
    strategy: "自动收矿挖矿、玩摇一摇、每日签到、各种任务",
    icon: "el-icon-game-tabizoo",
    gameUrl: "",
  },
  {
    bot: "memefi_coin_bot",
    name: "memefi",
    channel: "memeficlub",
    route: "/memefi/index",
    strategy: "自动收矿挖矿、玩摇一摇、每日签到、各种任务",
    icon: "el-icon-game-memefi",
    gameUrl: "",
  },  
  {
    bot: "cowscoin_bot",
    name: "cows",
    channel: "cowsclub",
    route: "/cows/index",
    strategy: "自动收矿挖矿、玩摇一摇、每日签到、各种任务",
    icon: "el-icon-game-cows",
    gameUrl: "",
  },

];

const Projects = {
  namespaced: true,
  state: () => ({
    // 账号是内存+indexedDB，账号状态持久化
    projectsList: null,
  }),
  mutations: {
    SET_PROJECTS_LIST(state, list) {
      // 这里的 `state` 对象是模块的局部状态
      state.projectsList = list
    }
  },
  actions: {
    async addProject({ dispatch }, project) {
      const dexie = await DexieCtx.db()
      const oldAc = await dexie.projects.get({
        phone: project.phone
      })
      if (oldAc == null) {
        await dexie.projects.add({ ...project })
      } else {
        project.id = oldAc.id
        await dispatch("updateProject", project)
      }
      await dispatch("loadProjects")
    },
    async updateProject({ dispatch }, project) {
      const dexie = await DexieCtx.db()
      let newProject = { ...project }
      await dexie.projects.where("id").equals(project.id).modify(newProject)
      await dispatch("loadProjects")
    },
    async loadProjects({ commit }) {
      const dexie = await DexieCtx.db()
      const arr = await dexie.projects.toArray()
      for (let index = 0; index < defaultProjectsData.length; index++) {
        const element = defaultProjectsData[index];
        const findResult = arr.find(item => item.bot == element.bot)
        if (findResult == null) {
          await dexie.projects.add(element)
        } else {
          await dexie.projects.where("id").equals(findResult.id).modify(element)
        }
      }
      const fixedArr = await dexie.projects.toArray()
      commit("SET_PROJECTS_LIST", fixedArr)
      return arr
    },
    async linkWallets({ state, dispatch }, walletList) {
      const dexie = await DexieCtx.db()
      for (let index = 0; index < state.projectsList.length; index++) {
        if (index >= walletList.length) {
          break
        }
        const project = state.projectsList[index];
        if (project.address != null) {
          continue
        }
        const wallet = walletList[index]
        project.address = wallet.address
        project.mnemonic = wallet.mnemonic
        project.address_user_friendly = wallet.address_user_friendly
        await dexie.projects.where("id").equals(project.id).modify(project)
      }
      await dispatch("loadProjects")
    }
  },
  getters: {
    projectsList(state) {
      return state.projectsList
    },
  }
}

export default Projects